using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.Diagnostics;
using Retrofit.Core;
using Retrofit.Input;

namespace Retrofit
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class MainGame : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        // Read-Only Values
        readonly int PaneCount;

        // Components
        PlayerManager players;
        
        RumbleManager rumbleManager;
       

        public MainGame()
        {
            graphics = new GraphicsDeviceManager(this);
            players = new PlayerManager(this);
            Content.RootDirectory = "Content";


            PaneCount = 2; // # of players.

#if !XBOX
            PaneCount = 1;
#endif
      
            Window.Title = "Mayhem Man"; // working title.
            IsMouseVisible = false;

            //TODO: Load settins from a file.
            SetScreenResolution(1280, 720, false);

            Components.Add(new GamerServicesComponent(this));
            Components.Add(players);

            Components.Add(rumbleManager = new RumbleManager(this));

        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            Log.WriteLine(LogCategory.Game, "Initializing...");
            players.PlayerJoinedGame += new Action<PlayerIndex>(players_PlayerJoinedGame);



            base.Initialize();
        }

        #region Event Handlers
        void players_PlayerJoinedGame(PlayerIndex index)
        {
            Log.WriteLine(LogCategory.Game, string.Format("Player {0} has joined.", index.ToString()));

            rumbleManager.AddRumble(index, new Retrofit.Library.RumbleSetting(0.5f, 0.5f, 500));

           SignedInGamer gamer = SignedInGamer.SignedInGamers[index];

           if (gamer != null)
           {

           }
           else 
           {
               Guide.ShowSignIn(PaneCount, false);
           }

        }

        #endregion

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            Log.WriteLine(LogCategory.Game, "Loading content...");
            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            UpdateDebugInput(); 
        

            

            // TODO: Add your update logic here

            base.Update(gameTime);
        }

       
  

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here

            base.Draw(gameTime);
        }

        protected override void OnExiting(object sender, EventArgs args)
        {
            base.OnExiting(sender, args);
            Log.WriteLine(LogCategory.Game, "Exiting...");
            Log.SaveFile("log");
        }

        #region Helper Functions
        public void SetScreenResolution(int width, int height, bool fullscreen)
        {
            graphics.PreferredBackBufferHeight = height;
            graphics.PreferredBackBufferWidth = width;
            graphics.IsFullScreen = fullscreen;
            graphics.ApplyChanges();
        }


        [Conditional("DEBUG")]
        public void UpdateDebugInput()
        {
            // This input code is just for debugging.

            if (Keyboard.GetState().IsKeyDown(Keys.F))
            {
                graphics.ToggleFullScreen();
            }

            if (Keyboard.GetState().IsKeyDown(Keys.Escape))
                this.Exit();
        }

        

      

        #endregion

        #region Entry Point
        static void Main(string[] args)
        {
            using (MainGame game = new MainGame())
            {
                game.Run();
            }
        }
        #endregion
    }
}
